<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * $Id: 2017-12-13 03:39:35 apple $
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
if($_RQ['op']=='list'){
	//开发文档
	$params=array();
	$params['type']=0;
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$orderby='id desc';
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('core_article',$params,array($pindex,$psize),$total,array('id','title','group_id','createtime'),'',$orderby);
	if($list){
		foreach($list as &$row){
			$row['group_id']=pdo_getcolumn('core_article_group',array('id'=>$row['group_id']),'title');
			
			$row['createtime']=date('20y-m-d H:i:s',$row['createtime']);
		}
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'新闻列表'),
			array('op'=>'notice','title'=>'公告列表'),
			array('op'=>'update','title'=>'更新列表'),
			array('op'=>'help','title'=>'手册列表'),
		),
		'list'=>array('data'=>'list','list'=>array(
			'0'=>array('name'=>'title','title'=>'新闻标题','type'=>'string'),
			'1'=>array('name'=>'group_id','title'=>'新闻分类','type'=>'string'),
			'2'=>array('name'=>'createtime','title'=>'创建时间','type'=>'string'),
			'3'=>array('op'=>'bbs_release','title'=>'同步发帖到论坛','type'=>'post'),
		)),
		'search_one'=>array('name'=>'title','add_query'=>'type=0'),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='help'){
	//手册
	$params=array();
	$params['type']=1;
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$orderby='id desc';
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('core_article',$params,array($pindex,$psize),$total,array('id','title','group_id','createtime'),'',$orderby);
	if($list){
		foreach($list as &$row){
			$row['group_id']=pdo_getcolumn('core_article_group',array('id'=>$row['group_id']),'title');
			
			$row['createtime']=date('20y-m-d H:i:s',$row['createtime']);
		}
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			'0'=>array('op'=>'list','title'=>'新闻列表'),
			'1'=>array('op'=>'notice','title'=>'公告列表'),
			'2'=>array('op'=>'update','title'=>'更新列表'),
			'3'=>array('op'=>'help','title'=>'手册列表'),
		),
		'list'=>array('data'=>'list','list'=>array(
			array('name'=>'title','title'=>'手册标题','type'=>'string'),
			array('name'=>'group_id','title'=>'手册分类','type'=>'string'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'string'),
			array('op'=>'yulan','title'=>'预览','type'=>'post'),
			array('op'=>'bbs_release','title'=>'同步发帖到论坛','type'=>'post'),
		)),
		'search_one'=>array('name'=>'title','add_query'=>'type=1'),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='yulan'){
	//预览手册文章
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要预览的文章！','error');
	}
	$url=SITEROOT.'manage/index.php?plugin=core&action=article.show&nosession=1&id='.$id;
	exi('','','',$url);
}
if($_RQ['op']=='notice'){
	//公告
	$params=array();
	$params['type']=2;
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$orderby='id desc';
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('core_article',$params,array($pindex,$psize),$total,array('id','title','group_id','createtime'),'',$orderby);
	if($list){
		foreach($list as &$row){
			$row['group_id']=pdo_getcolumn('core_article_group',array('id'=>$row['group_id']),'title');
			
			$row['createtime']=date('20y-m-d H:i:s',$row['createtime']);
		}
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			'0'=>array('op'=>'list','title'=>'新闻列表'),
			'1'=>array('op'=>'notice','title'=>'公告列表'),
			'2'=>array('op'=>'update','title'=>'更新列表'),
			'3'=>array('op'=>'help','title'=>'手册列表'),
		),
		'list'=>array('data'=>'list','list'=>array(
			'0'=>array('name'=>'title','title'=>'公告标题','type'=>'string'),
			'1'=>array('name'=>'group_id','title'=>'公告分类','type'=>'string'),
			'2'=>array('name'=>'createtime','title'=>'创建时间','type'=>'string'),
			'3'=>array('op'=>'bbs_release','title'=>'同步发帖到论坛','type'=>'post'),
		)),
		'search_one'=>array('name'=>'title','add_query'=>'type=2'),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='update'){
	//公告
	$params=array();
	$params['type']=3;
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	}
	$orderby='id desc';
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('core_article',$params,array($pindex,$psize),$total,array('id','title','group_id','createtime'),'',$orderby);
	if($list){
		foreach($list as &$row){
			$row['group_id']=pdo_getcolumn('core_article_group',array('id'=>$row['group_id']),'title');
			
			$row['createtime']=date('20y-m-d H:i:s',$row['createtime']);
		}
	}
	$return['list']=$list?$list:array();
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			'0'=>array('op'=>'list','title'=>'新闻列表'),
			'1'=>array('op'=>'notice','title'=>'公告列表'),
			'2'=>array('op'=>'update','title'=>'更新列表'),
			'3'=>array('op'=>'help','title'=>'手册列表'),
		),
		'list'=>array('data'=>'list','list'=>array(
			'0'=>array('name'=>'title','title'=>'公告标题','type'=>'string'),
			'1'=>array('name'=>'group_id','title'=>'公告分类','type'=>'string'),
			'2'=>array('name'=>'createtime','title'=>'创建时间','type'=>'string'),
			'3'=>array('op'=>'bbs_release','title'=>'同步发帖到论坛','type'=>'post'),
		)),
		'search_one'=>array('name'=>'title','add_query'=>'type=3'),
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item = pdo_get('core_article',array('id'=>$id));
	}else{
		$item=array('type'=>intval($_RQ['type']));
	}
	$type = $item['type'];
	$return['groups']=pdo_getall('core_article_group',array('type'=>$type),array('id','title'));
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'desc'=>'文章信息',
		'tabs'=>array(
			'0'=>array('op'=>'list','title'=>'新闻列表'),
			'1'=>array('op'=>'notice','title'=>'公告列表'),
			'2'=>array('op'=>'update','title'=>'更新列表'),
			'3'=>array('op'=>'help','title'=>'手册列表'),
			'4'=>array('op'=>'edit','title'=>'内容编辑'),
		),
		'edit'=>array(
			array('type'=>'string','title'=>'文章标题','name'=>'title', 'help'=>'' ,'must'=>true),
			array('type'=>'image','title'=>'文章LOGO','name'=>'image', 'help'=>''),
			array('type'=>'string','title'=>'文章排序','name'=>'paixu', 'help'=>'填写100以内正整数，数值越小越靠前' ),
			array('type'=>'switch','title'=>'是否显示','name'=>'status','help'=>'' ),
			array('type'=>'select','title'=>'文章分类','name'=>'group_id','options'=>'groups' ),
			array('type'=>'richtext','title'=>'文章内容','name'=>'content' , 'help'=>'','must'=>true),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	$data=array(
		'type'=>intval($post['type']),
		'title'=>trim($post['title']),
		'image'=>trim($post['image']),
		'paixu'=>intval($post['paixu']),
		'status'=>intval($post['status']),
		'group_id'=>intval($post['group_id']),
		'content'=>$post['content']
	);
	$id=intval($post['id']);
	if($id){
		pdo_update('core_article',$data,array('id'=>$id));
	}else{
		$data['createtime']=TIMESTAMP;
		pdo_insert('core_article',$data);
	}
	exi('保存成功！','','list');
}
if($_RQ['op']=='del'){
	$id=intval($_RQ['id']);
	if($id){
		pdo_delete('core_article',array('id'=>$id));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			if(intval($id)){
				pdo_delete('core_article',array('id'=>intval($id)));
			}
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='bbs_release'){
	//同步到论坛
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要同步的文章！','error');
	}
	$article=pdo_get('core_article',array('id'=>$id),array('title','content'));
	if(!$article){
		exi('指定文章不存在！','error');
	}
	$uc=core_setting('uc');
	$uc=unserialize(cfc('tools')->base64_decode($uc));
	if(!$uc || !$uc['up_status'] || !$uc['db_host'] || !$uc['db_user'] || !$uc['db_passwd'] || !$uc['db_name'] || !$uc['db_charset'] || !$uc['bbs_pre'] || !$uc['up_cid'] || !$uc['up_name'] || !$uc['up_uid']){
		exi('UC参数配置有误，无法同步发帖。如需发帖，请前往系统管理——系统设置——UC站点整合中配置好相关参数！','error');
	}
	$bbs_db=array(
		'host'=>$uc['db_host'],
		'username'=>$uc['db_user'],
		'password'=>$uc['db_passwd'],
		'database'=>$uc['db_name'],
		'charset'=>$uc['db_charset'],
		'tablepre'=>$uc['bbs_pre'],
		'port'=>3306,
	);
	require ROOT_D.'/core/class/bbs_release.php';
	$bbs_release=new bbs_release($bbs_db);
	$res=$bbs_release->post($uc['up_cid'], $article['title'], $article['content'], $uc['up_name'], $uc['up_uid'],0);
	exi($res,'','hold');
}
if($_RQ['op']=='get_article'){
	//列表
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$return['params']=$params=array();
	$params=array('status'=>1);
	$fields=array('title','id');
	if($_RQ['find']){
		$field=in_array($_RQ['field'],$fields)?$_RQ['field']:'title';
		$params[$field.' like']='%'.$_RQ['find'].'%';
		$return['params']['find']=$_RQ['find'];
		$return['params']['field']=$field;
	}
	$search_fields=array('id','createtime','pic_url','title');
	$list=pdo_getslice('core_account_article',$params,array($pindex,$psize),$total,$search_fields,'','id desc');
	if($list){
		foreach($list as &$item){
			//整理返回数据
			if($_RQ['many']){
				$item['res']=array('id'=>$item['id'],'title'=>$item['title'],'picurl'=>$item['pic_url']);
			}else{
				$item['res']=$item['id'];
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$return['htmls']=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'文章列表'),
		),
		'list'=>array('data'=>'list','del_one'=>false,'delall'=>false,'edit'=>false,'list'=>array(
		    array('name'=>'id','title'=>'文章id','type'=>'string'),
			array('name'=>'pic_url','title'=>'文章图片','type'=>'image'),
			array('name'=>'title','title'=>'文章标题','type'=>'string'),
			array('name'=>'createtime','title'=>'时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'modal_result','name'=>'res','title_field'=>'id','title'=>'选取'),
		)),
		'search_one'=>array('name'=>'find','select_name'=>'field','select_title'=>'筛选','is_radio'=>true,'options'=>'fields'),
	);
	$return['fields']=array(
		'id'=>'文章ID',
		'title'=>'文章标题',
	);
	exi($return);
}